﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataAccess;
namespace QLDSVCNTT_Nhom1
{
    public partial class FrmGiangVien : DevComponents.DotNetBar.Office2007Form
    {
        public FrmGiangVien()
        {
            InitializeComponent();
        }

        private void FrmGiangVien_Load(object sender, EventArgs e)
        {
            LoadGiangVien();
            //timer_Refresh.Enabled = true;
        }

        private void LoadGiangVien()
        {
            List<GiaoVien> list = GiaoVien.All();
            lvGiangVien.Items.Clear();
            int i = 1;
            foreach (GiaoVien gv in list)
            {
                ListViewItem item = lvGiangVien.Items.Add(i.ToString());
                item.SubItems.Add(gv.MaGV.ToString());
                item.SubItems.Add(gv.TenGV.ToString());
                item.SubItems.Add(gv.ChucVu.ToString());
                item.SubItems.Add(gv.HocVi.ToString());
                item.SubItems.Add(gv.Email.ToString());
                item.SubItems.Add(gv.Telephone.ToString());
                i++;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FrmThemGiangVien frm = new FrmThemGiangVien();
            frm.ShowDialog();
            LoadGiangVien();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lvGiangVien.SelectedItems.Count > 0)
            {
                string magv = lvGiangVien.SelectedItems[0].SubItems[1].Text;
                if (GiaoVien.Delete(magv) > 0)
                {
                    MessageBox.Show("Xóa thành công", "Thông báo");
                    LoadGiangVien();
                }
                else
                {
                    MessageBox.Show("Xóa không thành công, vui lòng kiểm tra lại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn giảng viên để xóa");
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (lvGiangVien.SelectedItems.Count >0)
            {
                GiaoVien gv = new GiaoVien();
                gv.MaGV = lvGiangVien.SelectedItems[0].SubItems[1].Text;
                gv.TenGV = lvGiangVien.SelectedItems[0].SubItems[2].Text;
                gv.ChucVu = lvGiangVien.SelectedItems[0].SubItems[3].Text;
                gv.HocVi = lvGiangVien.SelectedItems[0].SubItems[4].Text;
                gv.Email = lvGiangVien.SelectedItems[0].SubItems[5].Text;
                gv.Telephone = lvGiangVien.SelectedItems[0].SubItems[6].Text;
                FrmUpdateGiaoVien frm = new FrmUpdateGiaoVien();
                frm.GV = gv;
                frm.ShowDialog();
                LoadGiangVien();
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn giảng viên để sửa");
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            //timer_Refresh.Enabled = false;
            this.Close();
        }

        private void timer_Refresh_Tick(object sender, EventArgs e)
        {
            if (lvGiangVien.SelectedItems.Count > 0)
            {
                return;
            }
            else
            {
                LoadGiangVien();
            }
            
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            if (txtFind.Text != "" && cbbFind.SelectedItem != null)
            {
                //timer_Refresh.Enabled = false;
                string chuoitim = txtFind.Text;
                string chon = cbbFind.SelectedItem.ToString();
                TimKiem(chuoitim, chon);
            }
            else
            {
                MessageBox.Show("bạn chua nhập gì để tìm", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void TimKiem(string chuoitim, string chon)
        {
            List<GiaoVien> list = null;
            switch (chon)
            {
                case "Mã Giáo Viên":
                    lvGiangVien.Items.Clear();
                    list = GiaoVien.FindByMaGV(chuoitim);
                    LoadListView(list);
                    break;
                case "Tên Giáo Viên":
                    lvGiangVien.Items.Clear();
                    list = GiaoVien.FindByTenGV(chuoitim);
                    LoadListView(list);
                    break;
                case "Chức Vụ":
                    lvGiangVien.Items.Clear();
                    list = GiaoVien.FindByCVGV(chuoitim);
                    LoadListView(list);
                    break;
                case "Học Vị":
                    lvGiangVien.Items.Clear();
                    list = GiaoVien.FindByHVGV(chuoitim);
                    LoadListView(list);
                    break;
                default:
                    this.btnFind.PerformClick();
                    break;
            }
        }

        private void LoadListView(List<GiaoVien> list)
        {
            lvGiangVien.Items.Clear();
            int i = 1;
            foreach (GiaoVien item in list)
            {
                LayListView(item, i);
                i++;
            }
        }

        private void LayListView(GiaoVien item, int i)
        {
            ListViewItem lv = lvGiangVien.Items.Add(i.ToString());
            lv.SubItems.Add(item.MaGV.ToString());
            lv.SubItems.Add(item.TenGV.ToString());
            lv.SubItems.Add(item.ChucVu.ToString());
            lv.SubItems.Add(item.HocVi.ToString());
            lv.SubItems.Add(item.Email.ToString());
            lv.SubItems.Add(item.Telephone.ToString());
        }

        private void txtFind_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt16(e.KeyChar) == 13)
            {
                btnFind.PerformClick();
            }
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadGiangVien();
            //timer_Refresh.Enabled = true;
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            ListView root = lvGiangVien;
            string name = "DanhSachGiaoVien";
            saveFileDialog.Filter = "Save File(*.xls)|*.xls";
            saveFileDialog.FileName = "";
            saveFileDialog.Title = "Save to...";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                int num = SaveListviewToExcel.Export2Excel(root, System.IO.Path.GetFullPath(saveFileDialog.FileName),name);
                if (num == 1)
                {
                    MessageBox.Show("luu thanh cong");
                }
                else
                {
                    MessageBox.Show("Luu khong thanh cong");
                }
            }
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnDelete.PerformClick();
        }

        private void updateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnUpdate.PerformClick();
        }
        
    }
}
